package com.example.weather_plus.ui.weatherforecast.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.weather_plus.R;
import com.example.weather_plus.data.model.DailyWeatherForecast;
import com.google.android.material.progressindicator.LinearProgressIndicator;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class WeatherForecastAdapter extends RecyclerView.Adapter<WeatherForecastAdapter.WeatherForecastViewHolder> {
    private final List<DailyWeatherForecast> forecastList;

    public WeatherForecastAdapter(List<DailyWeatherForecast> forecastList) {
        this.forecastList = forecastList;
    }

    @NonNull
    @Override
    public WeatherForecastViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_weather_forecast, parent, false);
        return new WeatherForecastViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull WeatherForecastViewHolder holder, int position) {
        DailyWeatherForecast forecast = forecastList.get(position);
//        holder.tvDate.setText(forecast.getFxDate().toString());
        // 根据forecast.getWeatherCondition()设置holder.ivWeatherCondition的图标
        holder.tvMaxTemperature.setText(forecast.getTempMax());
        holder.tvMinTemperature.setText(forecast.getTempMin());


        if (position == 0) {

        } else {
            Date date = forecast.getFxDate();

            // 实例化Calendar并将Date设置进去
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            // 获取星期几
            int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
            // 注意：Calendar.SUNDAY = 1 表示周日，Calendar.MONDAY = 2 表示周一，以此类推
            String week = "周";

            switch (dayOfWeek) {
                case 1:
                    week += "日";
                    break;
                case 2:
                    week += "一";
                    break;
                case 3:
                    week += "二";
                    break;
                case 4:
                    week += "三";
                    break;
                case 5:
                    week += "四";
                    break;
                case 6:
                    week += "五";
                    break;
                case 7:
                    week += "六";
            }
            holder.tvDate.setText(week);
        }

//        holder.ivWeatherCondition.setImageResource();
        String cond = forecast.getTextDay();
        if (cond.equals("晴")) {
            holder.ivWeatherCondition.setImageResource(R.drawable.sunny);
        } else if (cond.equals("多云")) {
            holder.ivWeatherCondition.setImageResource(R.drawable.cloudy);
        } else if (cond.equals("阴")) {
            holder.ivWeatherCondition.setImageResource(R.drawable.yin);
        } else if (cond.equals("大雨")) {
            holder.ivWeatherCondition.setImageResource(R.drawable.heavy_rain);
        } else if (cond.equals("阵雨")) {
            holder.ivWeatherCondition.setImageResource(R.drawable.thunderstorm);
        } else if (cond.contains("雨")) {
            holder.ivWeatherCondition.setImageResource(R.drawable.rainy);
        } else if (cond.contains("霾")) {
            holder.ivWeatherCondition.setImageResource(R.drawable.haze);
        } else if (cond.contains("雾")) {
            holder.ivWeatherCondition.setImageResource(R.drawable.foggy);
        } else if (cond.contains("雪")) {
            holder.ivWeatherCondition.setImageResource(R.drawable.snow);
        } else if (cond.equals("冷")) {
            holder.ivWeatherCondition.setImageResource(R.drawable.cold);
        } else if (cond.contains("沙")) {
            holder.ivWeatherCondition.setImageResource(R.drawable.dust);
        } else if (cond.contains("雹")) {
            holder.ivWeatherCondition.setImageResource(R.drawable.hail);
        }
    }


    @Override
    public int getItemCount() {
        return forecastList.size();
    }

    public class WeatherForecastViewHolder extends RecyclerView.ViewHolder {
        public TextView tvDate, tvMinTemperature, tvMaxTemperature;
        public ImageView ivWeatherCondition;
        public LinearProgressIndicator pb;

        public WeatherForecastViewHolder(@NonNull View itemView) {
            super(itemView);
            tvDate = itemView.findViewById(R.id.tv_date);
            tvMinTemperature = itemView.findViewById(R.id.tv_min_temperature);
            tvMaxTemperature = itemView.findViewById(R.id.tv_max_temperature);
            ivWeatherCondition = itemView.findViewById(R.id.iv_weather_condition);

        }
    }
}
